package com.yunhe.tianhe.rentcars.service.impl;

import com.yunhe.tianhe.rentcars.dao.permission.AccountDao;
import com.yunhe.tianhe.rentcars.dao.permission.ResourceDao;
import com.yunhe.tianhe.rentcars.entity.permission.Account;
import com.yunhe.tianhe.rentcars.entity.permission.Resource;
import com.yunhe.tianhe.rentcars.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/10/16 0016 上午 10:48
 * @Description: 登陆服务
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private AccountDao accountDao;

    public Account findByUser (String userName, String userPwd)   {
        Account account = accountDao.findByUser(userName, userPwd);
        return account;
    }

    /*public List<PermissionDto> permissionMenu(Long userId){

        List<PermissionDto> permissionMenu = new ArrayList<>();
        PermissionDto permissionDto = new PermissionDto();
        List<Permission> permissionList = permissionDao.findPermissionMenu(userId);
        if (null!=permissionList){
            for (int i = 0; i< permissionList.size();i++){
                Permission permission = permissionList.get(i);
                List<PermissionDto> subPermissionMenu = new ArrayList<>();
                if (permission.getType()==1){
                    Long parentId = permission.getId();
                    for (int j = 0; j< permissionList.size();j++){
                        Permission permission1 = permissionList.get(j);
                        if (permission1.getParentId()==parentId){
                            permissionDto = new PermissionDto(permission1.getId(),permission1.getName(),permission1.getUri(),permission1.getType());
                            subPermissionMenu.add(permissionDto);
                        }
                    }
                    permissionDto = new PermissionDto(permission.getId(),permission.getName(),permission.getUri(),permission.getType(),subPermissionMenu);
                    permissionMenu.add(permissionDto);
                }
            }
        }
        return permissionMenu;
    }*/


    @Override
    public Map<String, Resource> allPermission(Long userId) {
        List<Resource> permissionList = resourceDao.allPermission(userId);
        Map<String,Resource> permissionMap = new HashMap();
        if (null!=permissionList) {
            for (int i = 0; i < permissionList.size(); i++) {
                Resource p = permissionList.get(i);
                permissionMap.put(p.getRelateUrl(),p);
            }
        }
        return permissionMap;
    }

}
